-// $Id: serial_win.cpp,v 1.1 2009/09/02 19:05:27 robertl Exp $
+// $Id: serial_win.cpp,v 1.2 2009/11/19 03:56:03 robertl Exp $
//------------------------------------------------------------------------
//
// Copyright (C) 2009 S. Khai Mong <khai@mangrai.com>.
#include "mainwindow.h"
+#if 0 // Does not require Windows 2000
+
static const char *deviceNames[] = {
"com1:",
"com2:",
box->addItem(deviceNames[i]);
}
}
+
+#else // This code assumes Windows 2000 or later
+
+// Uses QueryDosDevice(), Minimum supported: Windows 2000 Professional/Server
+#include <Windows.h>
+#include <stdio.h>
+
+void MainWindow::osLoadDeviceNameCombos(QComboBox *box)
+{
+ char DevList[64*1024-1]; // a single byte more, and certain versions of windows
+ // always return QueryDosDevice()==0 && GetLastError()==ERROR_MORE_DATA.
+ // see http://support.microsoft.com/kb/931305
+ // Get a list of all existing MS-DOS device names. Stores one or more asciiz strings followed by an extra null.
+ DWORD res = QueryDosDeviceA(NULL, DevList, sizeof(DevList));
+ if (res == 0)
+ {
+ DWORD err = GetLastError(); // could check for ERROR_INSUFFICIENT_BUFFER, and retry with a larger buffer.
+ // but DevList is already at the maximum size it can be without running into kb 931305.
+ // FIXME: This shold be a QMessageBox::warning() - RJL
+ // fprintf(stderr,"QueryDosDevice() failed with %d. GetLastError()==%d.\n", res, err);
+ return;
+ }
+
+ for (char *p=DevList; *p;) {
+ int len = strlen(p);
+ if (strncmp(p,"COM",3)==0)
+ box->addItem((PCHAR)p);
+ p += len+1; // +1 to also skip the null character of each string
+ }
+}
+
+#endif